package com.hgj.web;

import com.alibaba.fastjson.JSON;
import com.hgj.dao.CommentDao;
import com.hgj.dao.GoodsDao;
import com.hgj.dao.impl.CommentDaoImpl;
import com.hgj.dao.impl.GoodsDaoImpl;
import com.hgj.entity.Comment;
import com.hgj.entity.Goods;
import com.hgj.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods/*")
public class GoodsServlet extends HttpServlet {
    private GoodsDao goodsDao=new GoodsDaoImpl();
    private CommentDao commentDao=new CommentDaoImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取项目基准路径
        String baseURL = request.getContextPath();
        //截取项目二级路径
        String uri = request.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        System.out.println("path = " + path);
        if ("find.do".equals(path)) { //列表显示
            String goods_type=request.getParameter("goods_type");
            String goods_name=request.getParameter("goods_name");
            Map<String,Object> map= new HashMap<String,Object>();
            if(StringUtil.isNotEmpty(goods_type)){
                map.put("goods_type",goods_type);
            }
            if(StringUtil.isNotEmpty(goods_name)){
                map.put("goods_name",goods_name);
            }
            List<Goods> goodsList=goodsDao.find(map);
            //设置返回json格式
            response.setContentType("application/json;charset=utf-8");
            String json=JSON.toJSONString(goodsList);//转json
            PrintWriter out=response.getWriter();//获取输出
            out.print(json);
            out.close();
            return;
        } else if("showList.do".equals(path)){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("goods_type", 0); //参数传递  加载一级分类
                List<Goods> goodsList = goodsDao.find(map);
                request.setAttribute("goodsList", goodsList);
                request.getRequestDispatcher(baseURL + "mobile/category.jsp").forward(request, response);

        }else if ("con.do".equals(path)){
            //根据id查询商品详情
            String id = request.getParameter("goods_id");
            String comment=request.getParameter("comment_id");
          //  String goods_intro=request.getParameter("goods_intro");
            Goods goods = goodsDao.findById(id);
            request.setAttribute("goods", goods);
            Comment comment1=commentDao.findById(comment);
            request.setAttribute("comment1",comment1);
            request.getRequestDispatcher(baseURL + "/mobile/detail.jsp").forward(request, response);
        }
        else {
            throw new RuntimeException("请求路径不存在");

        }

    }
}
